﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using Fusion;
using Fusion.Interface;

namespace Fusion.Tools
{
	public class CrossSelector : Entity
	{
		#region Fields

		// max dimensions
		int width = 3;
		int height = 3;

		// center position
		int xCenter = 1;
		int yCenter = 1;

		// individual col/row sizes
		bool horizontal = false;
		List<int> sizes = new List<int>();

		// number of rows/cols to draw
		int dislpayCols = 3;
		int displayRows = 3;

		public Vector2 target = Vector2.Zero;
		Vector2 display = Vector2.Zero;

		public Vector2 drawSize = Vector2.One * 64;
		public Vector2 drawSpan = Vector2.One * 80;

		public Button leftButton;
		public Button rightButton;

		#endregion

		#region Events

		void MoveLeft()
		{
			target.X--;
		}

		void MoveRight()
		{
			target.X++;
		}

		#endregion

		#region Constructors

		/// <summary> Default Constructor </summary>
		public CrossSelector()
		{
			leftButton = new Button( new Vector2( -128, -240 ), Vector2.One * 24, "Buttons", new Rectangle(0,0,64,64), MoveLeft, null );
			rightButton = new Button( new Vector2( 128, -240 ), Vector2.One * 24, "Buttons", new Rectangle( 0, 0, 64, 64 ), MoveRight, null );
			leftButton.container = this;
			rightButton.container = this;
		}

		#endregion

		#region Methods

		public void DoInput( Vector2 cursor, bool btnA, bool btnB, bool btnC, int wheelDirection )
		{
			leftButton.Control( cursor, btnA );
			rightButton.Control( cursor, btnB );
		}

		/// <summary> Update method </summary>
		/// <param name="period">Section fraction since last update</param>
		public override void Update()
		{
			float period = Engine.period;

			leftButton.Update();
			rightButton.Update();

			// move display towards target
			display = Hax.Lerp( display, target, Engine.period * 8 );

		}

		/// <summary>
		/// Draws a specified row, windowing the list of entities
		/// </summary>
		/// <param name="row">The row to draw</param>
		/// <param name="entities">The entities to draw along the row</param>
		public void Draw( List<List<Entity>> lists )
		{
			// Stay within bounds
			target.X = Hax.Clamp( target.X, 0, lists.Count - 1 );
			if( lists.Count > 0 )
				target.Y = Hax.Clamp( target.Y, 0, lists[(int)target.X].Count - 1 );

			// Draw rear window
			UI.DrawBlock( position, new Vector2( 256, 512 ), 0, new Color( 0, 0, 0, 0.64f ) );

			// Draw the entities

			int start = (int)display.Y - 2;
			Vector2 colMover = Vector2.Zero;

			for( int col=0; col < lists.Count; col++ )
			{
				float colDif = col - display.X;
				if( colDif >= -1.6f && colDif <= 1.6f )
				{

					float colScaler = 1 - ( colDif * colDif * 0.08f );
					if( colScaler < 0 ) colScaler = 0;

					colMover.X = ( colDif * 48 * ( colScaler * 2 ) );

					int iterCol=0;
					while( iterCol < 5 )
					{
						int i = start; // draw in the correct order
						if( iterCol == 0 ) i += 0;
						if( iterCol == 1 ) i += 4;
						if( iterCol == 2 ) i += 1;
						if( iterCol == 3 ) i += 3;
						if( iterCol == 4 ) i += 2; // current

						if( i >= 0 && lists[col].Count > i )
						{
							lists[col][i].container = this;

							float dif = i - display.Y;

							float s = 1 - ( dif * dif * 0.08f );
							if( s < 0 ) s = 0;
							s *= colScaler;

							colMover.Y = dif * 64 * ( s * 2 );

							lists[col][i].Draw( drawSpan + colMover + new Vector2( -80, -80 ), Vector2.One * s, 0 );
						}
						iterCol++;
					}
				}
			}

			// Draw buttons
			leftButton.Draw();
			rightButton.Draw();
		}

		/// <summary>
		/// Draws a specified column, windowing the list of entities
		/// </summary>
		/// <param name="col">The column to draw</param>
		/// <param name="entities">The entities to draw along the column</param>
		public void DrawColumn( int col, List<Entity> entities )
		{
			// update colum heights
			horizontal = false;
			while( sizes.Count <= col )
				sizes.Add( 0 );
			sizes[col] = entities.Count;

			if( width < col )
				width = col;



			// Calculate bounds


			// Draw the entities
			int start = (int)display.Y - 2;

			int iter=0;
			while( iter < 5 )
			{
				int i = start; // draw in the correct order
				if( iter == 0 ) i += 0;
				if( iter == 1 ) i += 4;
				if( iter == 2 ) i += 1;
				if( iter == 3 ) i += 3;
				if( iter == 4 ) i += 2; // current

				if( i >= 0 && entities.Count > i )
				{
					float dif = i - display.Y;
					entities[i].container = this;
					float s = 1 - ( dif * dif * 0.08f );
					if( s < 0 ) s = 0;

					entities[i].Draw( drawSpan + new Vector2( 0, dif * 64 * ( s * 2 ) ), Vector2.One * s, 0 );
				}
				iter++;
			}
		}

		#endregion
	}
}
